package com.easyCode;

import com.easyCode.dataMock.DataMockManager;
import com.easyCode.mysql.mock.MysqlMockSqlParse;
import com.easyCode.mysql.sqlBuilder.sqlInfo.MysqlDataBaseSqlInfo;
import com.easyCode.mysql.sqlExec.MysqlSqlExecImpl;
import com.easyCode.sqlBuilder.sqlInfo.BaseDataBaseSqlInfo;
import com.easyCode.sqlMapper.mapInfoAdaptor.DataBaseMapInfoAdaptor;
import com.easyCode.sqlExecer.SqlExec;
import com.easyCode.sqlManager.GlobalProperties;
import com.easyCode.sqlManager.SqlManager;
import com.easyCode.sqlMapper.SqlMapper;
import com.easyCode.sqlMapper.mapInfoAdaptor.MapInfoAdaptor;
import com.easyCode.sqlMapper.template.SqlTemplate;
import com.easyCode.sqlMapper.template.SqlTemplateConfig;
import com.easyCode.sqlMapper.template.VelocitySqlTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author 大忽悠
 * @create 2022/12/18 12:55
 */
@Configuration
@ConditionalOnProperty(prefix = "easy-generator",name = "open",havingValue = "true")
public class EasyGeneratorAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    public SqlManager sqlManager(SqlExec sqlExec, SqlMapper sqlMapper, MapInfoAdaptor<?> mapInfoAdaptor, GlobalProperties globalProperties,DataSourceProperties dataSourceProperties
                                 ,BaseDataBaseSqlInfo baseDataBaseSqlInfo,DataMockManager dataMockManager) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        return new SqlManager(sqlExec,sqlMapper,mapInfoAdaptor,globalProperties,dataSourceProperties,baseDataBaseSqlInfo,dataMockManager);
    }

    @Bean
    @ConditionalOnMissingBean
    public SqlMapper sqlMapper(SqlTemplate sqlTemplate){
        return new SqlMapper(sqlTemplate);
    }

    @Bean
    @ConditionalOnMissingBean
    public SqlTemplate sqlTemplate(SqlTemplateConfig sqlTemplateConfig){
        return new VelocitySqlTemplate(sqlTemplateConfig);
    }

    @Bean
    @ConditionalOnMissingBean
    public SqlTemplateConfig sqlTemplateConfig(){
        return new SqlTemplateConfig();
    }

    @Bean
    @ConditionalOnMissingBean
    public DataBaseMapInfoAdaptor dataBaseMapInfoAdaptor(){
        return new DataBaseMapInfoAdaptor();
    }

    @Bean
    @ConditionalOnMissingBean
    public GlobalProperties globalProperties(){
        return new GlobalProperties();
    }


    @Bean
    @ConditionalOnMissingBean
    public DataMockManager dataMockManager(){
        return new DataMockManager();
    }

    // 默认支持的是mysql数据库,用户可自行实现并替换如下两个Bean,完成动态数据源切换

    @Bean
    @ConditionalOnMissingBean
    public BaseDataBaseSqlInfo baseDataBaseSqlInfo(){
        return new MysqlDataBaseSqlInfo();
    }

    @Bean
    @ConditionalOnMissingBean
    public SqlExec sqlExec(JdbcTemplate jdbcTemplate){
        return new MysqlSqlExecImpl(jdbcTemplate);
    }

    @Bean
    @ConditionalOnMissingBean
    public MysqlMockSqlParse mockSqlParse(){
        return new MysqlMockSqlParse();
    }
}
